<template>
  <div class="detailContainer">
    <!-- 头部 -->
    <div class="bg">
      <div class="header">
        <div class="img home" @click="skip('home')"></div>
        <div class="img title"></div>
        <div class="right">
          <div class="img loupe" @click="skip('search')"></div>
          <div class="img shopcard" @click="skip('shopcard')"></div>
        </div>
      </div>
    </div>
    <!-- 轮播 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in skuInfo.skuImageList" :key="item.id"
        ><img :src="item.imgUrl" alt=""
      /></van-swipe-item>
    </van-swipe>
    <!-- 价格 -->
    <div class="bargain_price">
      <div class="left">
        <div class="top">
          <span>限时优惠</span>
          &nbsp;&nbsp;|&nbsp;&nbsp;
          <span>距结束1天 </span>
          <van-count-down
            millisecond
            :time="time"
            format="HH:mm:ss:S"
            class="timer"
          />
          <!-- <span class="SS">{{ time_S }}</span> -->
        </div>
        <div class="bottom">
          <span class="dl">￥</span>
          <span class="money">{{ skuInfo.price }}</span>
          <span class="yj_money">￥{{ (skuInfo.price * 1.1).toFixed(2) }}</span>
        </div>
      </div>
      <div class="right">
        <img class="" src="../../assets/images/nhj.png" alt="" />
      </div>
    </div>
    <div class="goods_detial">
      <div class="pro">
        <div class="left">
          <img class="pro_img" src="../../assets/images/pro.png" alt="" />
          <span>Pro会员叠加优惠，再省</span>
          <p class="money">￥{{ (skuInfo.price * 0.01).toFixed(2) }}</p>
          <p class="iconfont icon-xiangqing"></p>
        </div>
        <div class="right">开通</div>
      </div>
      <div class="baseInfo">
        <div class="left">
          <div class="name">
            {{ skuInfo.skuName }}
          </div>
          <div class="distribution_channel">
            <img class="prelogo" src="../../assets/images/zy.png" />
            <img src="../../assets/images/lq.png" alt="" />
            <span>网易严选</span>
          </div>
          <span>推荐理由</span>
        </div>
        <div class="right">
          <div class="cationer">
            <div class="text">
              <span>99.7%</span>
              <p>好评率</p>
            </div>
            <p class="iconfont icon-youjiantou"></p>
          </div>
        </div>
      </div>
      <div class="rcmdbanner">
        <ul>
          <li>
            <div class="key">1</div>
            <div class="recommendReason">好用</div>
          </li>
          <li>
            <div class="key">2</div>
            <div class="recommendReason">非常好用</div>
          </li>
          <li>
            <div class="key">3</div>
            <div class="recommendReason">非常非常好用</div>
          </li>
          <li>
            <div class="key">4</div>
            <div class="recommendReason">非常非常非常好用</div>
          </li>
        </ul>
      </div>
    </div>
    <div class="footer">
      <div class="cationer">
        <i class="iconfont icon-kefu"></i>
        <button class="btn btn1">立即购买</button>
        <button class="btn btn2" @click="showPopup">加入购物车</button>
      </div>
    </div>
    <div class="popup_cationer"></div>
    <van-popup
      v-model="show"
      :style="{ height: '73%' }"
      position="bottom"
      :get-container="getContainer"
      closeable
      class="popup"
    >
      <div class="info_con">
        <img :src="skuInfo.skuDefaultImg" alt="" />
        <div class="right_f">
          <span class="cz">超值捡漏</span>
          <p class="price">
            价格：￥{{ (skuInfo.price * 1.1).toFixed(2) }}&nbsp;到手价￥{{
              skuInfo.price
            }}
          </p>
          <p>
            已选择：{{ selectced_C }}&nbsp;{{ selected_B }}
            <span v-show="!selectced_C">请选择规格数量</span>
            <span v-show="selectedDetail[0]">
              <p v-for="s in selectedDetail" :key="s.id">
                {{ s.color || s.edition }}
              </p>
            </span>
          </p>
        </div>
      </div>
      <div class="parameter" v-for="spu in spuSaleAttrList" :key="spu.id">
        <div class="tt">{{ spu.saleAttrName }}</div>
        <div class="con">
          <div
            class="p_item"
            :class="{
              active:
                selectced_C === pu_C.saleAttrValueName ||
                selected_B === pu_C.saleAttrValueName,
            }"
            v-for="pu_C in spu.spuSaleAttrValueList"
            :key="pu_C.id"
            @click="isSelect(pu_C)"
          >
            {{ pu_C.saleAttrValueName }}
          </div>
        </div>
      </div>

      <div class="num">
        <div class="tt">数量</div>
        <div class="con">
          <div class="changenum" @click="changeNum(-1)" :disabled="ischange">
            -
          </div>
          <input class="ipt" type="text" v-model="goodsNum" name="" id="" />
          <div class="changenum" @click="changeNum(1)">+</div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import { Dialog, Toast } from "vant";
import { mapGetters } from "vuex";
export default {
  name: "Detail",
  data() {
    return {
      show: false,
      time: 4 * 60 * 60 * 1000,
      goodsNum: 1,
      goodsList: [15, 21, 2, 12, 10, 3],
      selectced_C: "",
      selected_B: "",
      selected_id: "",
      selectedDetail: [],
      ischange: true,
    };
  },
  mounted() {
    let goodsId = parseInt(Math.random() * 5);
    this.getGoodsDetail(this.goodsList[goodsId]);
  },
  methods: {
    // 点击加入购物车，是否显示弹窗
    async showPopup() {
      // localStorage.setItem("token_key", "f0571b8b61f045b29d9bad9faa7807fd");
      // localStorage.setItem(
      //   "userTempId",
      //   "46a7e6bf-7832-404b-9015-97f75f6742b6"
      // );

      if (this.selectced_C && this.selected_B) {
        let token = localStorage.getItem("token_key");
        if (token) {
          let res = await this.$store.dispatch("addShopCard", {
            skuId: this.selected_id,
            skuNum: this.goodsNum,
          });
          res === "ok" && Toast("添加成功");
        } else {
          this.$router.push({ name: "login" });
        }
      } else {
        this.show && Toast("请选择想要商品参数");
      }
      this.show = true;
    },
    // 返回一个特定的 DOM 节点，作为挂载的父节点
    getContainer() {
      return document.querySelector(".popup_cationer");
    },
    // 获取商品详情
    getGoodsDetail(goodsId) {
      this.$store.dispatch("gitDeatilData", goodsId);
    },
    // 点击选择
    isSelect(goods) {
      goods.saleAttrName === "颜色"
        ? (this.selectced_C = goods.saleAttrValueName)
        : (this.selected_B = goods.saleAttrValueName);
      this.selected_id = this.skuInfo.id;
    },
    changeNum(num) {
      this.goodsNum += num;
      if (this.goodsNum < 1) {
        this.goodsNum = 0;
        this.ischange = false;
      }
    },
    skip(address) {
      if (address === "home") {
        this.$router.push({
          path: "/search",
          query: {
            redirect: "/detail",
          },
        });
      } else if (address === "search") {
        this.$router.push({ name: "search" });
      } else {
        this.$router.push({ name: "cart" });
      }
    },
  },
  computed: {
    ...mapGetters(["categoryView", "skuInfo", "spuSaleAttrList"]),
    isSelectedGoodsDetail() {
      let { selectced_C, selected_B } = this;
      return;
    },
  },
};
</script>
<style lang="less" scoped>
@import url("../../assets/font/resetcss.css");
// 头部
.bg {
  height: 44px;
  .header {
    background-color: white;
    display: flex;
    width: 100%;
    height: 44px;
    justify-content: space-between;
    padding: 0 10px;
    align-items: center;
    position: fixed;
    border-bottom: 1px solid rgba(170, 170, 170, 0.363);
    z-index: 999;
    .img {
      background-image: url("../../assets/images/jlt.png");
      background-size: 115px 100px;
      background-repeat: no-repeat;
    }
    .home {
      height: 22px;
      width: 24px;
      background-position: 0px -78px;
    }
    .title {
      background-position: 0px 0px;
      width: 86px;
      height: 27px;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      margin: auto;
    }
    .right {
      display: flex;
      .loupe {
        height: 32px;
        width: 32px;
        background-position: -85px 0px;
        margin-right: 10px;
      }
      .shopcard {
        height: 32px;
        width: 32px;
        background-position: -35px -28px;
      }
    }
  }
}
// 轮播图
.my-swipe {
  height: 375px;
  .van-swipe-item {
    color: #fff;
    font-size: 20px;
    line-height: 150px;
    text-align: center;
    background-color: #39a9ed;
    img {
      width: 375px;
      height: 375px;
    }
  }
}
// 特价
.bargain_price {
  height: 74px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-left: 15px;
  background-color: #fa1e32;
  .left {
    width: 285px;
    height: 74px;
    padding-top: 8px;
    .top {
      font-size: 12px;
      display: flex;
      color: white;
      display: flex;
      align-items: center;
      margin-bottom: 4px;
    }
    // .SS {
    //   width: 14px;
    //   height: 14px;
    //   background-color: white;
    //   color: red;
    //   border-radius: 50%;
    //   text-align: center;
    //   line-height: 14px;
    // }
    .timer {
      color: white;
    }
    .bottom {
      color: white;
      font-weight: 600;
      .dl {
        font-size: 18px;
      }
      .money {
        font-size: 32px;
      }

      .yj_money {
        font-size: 14px;
        text-decoration-line: line-through;
      }
    }
  }
  .right {
    img {
      width: 75px;
      height: 40px;
    }
  }
}
.goods_detial {
  padding: 13px 0px 18px 15px;
  .pro {
    width: 345px;
    height: 40px;
    background-color: #fff1d2;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    margin: 8px 0px 12px 0px;
    .left {
      display: flex;
      font-size: 12px;
      height: 34px;
      display: flex;
      align-items: center;
      .pro_img {
        width: 21px;
        height: 12px;
      }
      span {
        font-weight: 600;
        margin-left: 2px;
      }
      .money {
        color: #dd1a71;
        font-weight: 600;
      }
      .iconfont {
        color: #aaa;
        padding: 1.5px 0px 0px 2px;
      }
    }
    .right {
      width: 48px;
      height: 24px;
      border-radius: 12px;
      text-align: center;
      line-height: 24px;
      background-color: black;
      color: #f3ca77;
    }
  }
  .baseInfo {
    display: flex;
    justify-content: space-between;
    height: 89px;
    .left {
      width: 271px;
      height: 89px;
      display: flex;
      flex-direction: column;
      .name {
        font-size: 16px;
        font-weight: 600;
        width: 259px;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        padding-bottom: 5px;
      }
      .distribution_channel {
        display: flex;
        height: 28px;
        align-items: center;
        margin: 3px 0px;
        .prelogo {
          width: 32px;
          height: 16px;
          margin-right: 4px;
        }
        img {
          width: 14px;
          height: 14px;
          margin-right: 4px;
        }
      }
      span {
        color: #aaa;
      }
    }
    .right {
      .cationer {
        display: flex;
        justify-content: center;
        align-items: center;
        .iconfont {
          color: #aaa;
          margin: 0px 12px 0px 7px;
        }
        .text {
          span {
            font-size: 16px;
            color: #dd1a21;
            font-weight: 600;
          }
          p {
            margin-top: 5px;
            font-size: 12px;
            color: #aaa;
          }
        }
      }
    }
  }
  .rcmdbanner {
    border: 1px solid #aaa;
    padding: 7px 10px;
    margin: 6px 15px 0px 0px;
    ul {
      li {
        font-size: 12px;
        .key {
          display: inline-block;
          width: 12px;
          height: 12px;
          color: #dd1a21;
          border: 2px solid #dd1a21;
          border-radius: 6px;
          font-weight: 600;
          text-align: center;
          line-height: 10px;
          margin-right: 4px;
        }
        .recommendReason {
          display: inline-block;
        }
      }
    }
  }
}
.footer {
  height: 52px;
  .cationer {
    position: fixed;
    bottom: 0;
    background-color: white;
    border-top: 1px solid #ddd;
    display: flex;
    justify-content: space-between;
    z-index: 999;
    .icon-kefu {
      width: 78px;
      height: 52px;
      color: #aaa;
      text-align: center;
      line-height: 52px;
      font-size: 30px;
      border-right: 1px solid #ddd;
    }
    .btn {
      width: 158.5px;
      height: 52px;
      border: 0;
      background-color: transparent;
      outline: 0;
    }
    .btn1 {
      background-color: white;
    }
    .btn2 {
      color: white;
      background-color: #dd1a21;
    }
  }
}
.popup_cationer {
  position: fixed;
  // bottom: 52px;
}
// 弹出
.popup {
  padding: 16.5px 15px 63px;
  .info_con {
    width: 345px;
    height: 99px;
    display: flex;
    margin-bottom: 21px;
    img {
      height: 99px;
      width: 98px;
    }
    .right_f {
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      .cz {
        width: 60px;
        height: 17px;
        text-align: center;
        line-height: 17px;
        font-size: 12px;
        margin-left: 7px;
        color: white;
        background-color: #f48f18;
        border-radius: 3px;
        padding: 0 5px;
      }
      p {
        font-size: 14px;
      }
      .price {
        color: #e41a21;
        margin: 3px 0px;
      }
    }
  }
  .parameter {
    font-size: 14px;
    .con {
      display: flex;
      flex-wrap: wrap;
      margin-top: 6px;
      padding-bottom: 10px;
      .p_item {
        padding: 0px 12px;
        height: 36px;
        text-align: center;
        line-height: 36px;
        box-sizing: border-box;
        border: 1px solid #aaa;
        border-radius: 3px;
        flex-shrink: 0;
        margin: 0px 10px 10px 0px;
        &.active {
          border: 1px solid red;
          color: red;
        }
      }
    }
  }
  .num {
    font-size: 14px;
    .con {
      display: flex;
      border: 1px solid #aaa;
      justify-content: flex-start;
      border-radius: 3px;
      width: 154px;
      .changenum {
        width: 45px;
        height: 33px;
        text-align: center;
        line-height: 33px;
      }
      .ipt {
        width: 64px;
        height: 33px;
        border-left: 1px solid #aaa;
        border-right: 1px solid #aaa;
        text-align: center;
        line-height: 33px;
      }
    }
  }
}
</style>
